package cn.shyouchi.wuguanjia.android.ui.meetting.plan;

import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.meet.MeetApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.MeetCheckInEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.ui.meetting.common.MeetPerson;
import cn.shyouchi.wuguanjia.android.utils.DisplayUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ScanSignMeetActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.img_qrcode)
    ImageView mImgQrcode;
    @BindView(R.id.tv_sign_count)
    TextView mTvSignCount;
    @BindView(R.id.rv_sign_person)
    RecyclerView mRvSignPerson;
    int meetId;
    MeetApi mMeetApi;

    MeetSignPersonAdapter mMeetSignPersonAdapter;

    List<MeetPerson> mMeetPersons = new ArrayList<>();

    @Override
    public int initContentView() {
        return R.layout.activity_scan_sign_meet;
    }

    @Override
    public void initInjector() {
        meetId = getIntent().getIntExtra("meetId", 0);
        mMeetApi = new MeetApi(mContext);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);

        createQRCode();
        fetchMeetSignPerson();


        mMeetSignPersonAdapter = new MeetSignPersonAdapter(mMeetPersons);
        mRvSignPerson.setAdapter(mMeetSignPersonAdapter);
        mRvSignPerson.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    }


    /**
     * 生成签到二维码
     */
    private void createQRCode() {
        Uri uri = Uri.parse("meet://checkin?meetId=" + meetId);
        Observable.just(uri.toString())
                .subscribeOn(Schedulers.newThread())
                .map(new Func1<String, Bitmap>() {
                    @Override
                    public Bitmap call(String s) {
                        return QRCodeEncoder.syncEncodeQRCode(s, DisplayUtil.dip2px(mContext, 200));
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Bitmap>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Bitmap bitmap) {
                        mImgQrcode.setImageBitmap(bitmap);
                    }
                });
    }


    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPersonSign(MeetCheckInEvent event) {
        fetchMeetSignPerson();
    }


    private void fetchMeetSignPerson() {
        mMeetApi.fetchMeetCheckPerson(meetId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseRequestResult<List<MeetPerson>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(BaseRequestResult<List<MeetPerson>> result) {
                        mMeetPersons.clear();
                        mMeetPersons.addAll(result.getResultMessage());
                        mMeetSignPersonAdapter.notifyDataSetChanged();
                        mTvSignCount.setText(String.format("签到情况（%s）", mMeetPersons.size()));
                    }
                });
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);

        super.onDestroy();
    }
}
